<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>宿舍管理系统</title>
    <meta name="renderer" content="webkit">
    <meta name="csrf-token" content="{{ csrf_token()}}"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>

    <link rel="stylesheet" href="{{asset('css/font.css')}}">
    <link rel="stylesheet" href="{{asset('css/xadmin.css')}}">
    <script type="text/javascript" src="{{asset('lib/layui/layui.js')}}" charset="utf-8"></script>
    <script type="text/javascript" src="{{asset('js/xadmin.js')}}"></script>

    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            {{csrf_field()}}
            <input type="hidden" name="staff_id" value="{{request()->get('staff_id')}}">
            <div class="layui-form-item">
                <label for="name" class="layui-form-label">
                    <span class="x-red">*</span>姓名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="name" name="name" required="" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="id_card" class="layui-form-label">
                    身份证号
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="id_card" name="id_card"  lay-verify="identity" onblur="GetAge(this)"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="age" class="layui-form-label">年龄</label>
                <div class="layui-input-block">
                    <input type="number" name="age" id="age" lay-verify="number" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="relationship" class="layui-form-label">与员工关系</label>
                <div class="layui-input-block">
                    <select name="relationship" lay-search>
                        <option value="配偶">配偶</option>
                        <option value="子女">子女</option>
                        <option value="父母">父母</option>
                        <option value="其他">其他</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="sex" class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="1" title="男">
                    <input type="radio" name="sex" value="2" title="女">
                    <input type="radio" name="sex" value="" title="待定" checked>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="phone" class="layui-form-label">电话</label>
                <div class="layui-input-block">
                    <input type="text" name="phone" placeholder="请输入手机或电话号码" lay-verify="phonemsg" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="status" class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="radio" name="status" value="0" title="搬出">
                    <input type="radio" name="status" value="1" title="寄宿" checked>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="dorm_type" class="layui-form-label">入住类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="dorm_type" value="常住" title="常住" checked>
                    <input type="radio" name="dorm_type" value="偶尔" title="偶尔">
                    <input type="radio" name="dorm_type" value="临时" title="临时">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="remark" class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <input type="text" name="remark" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label">
                </label>
                <button class="layui-btn" lay-filter="add" lay-submit="">
                    添加
                </button>
            </div>
        </form>
    </div>
</div>
<script>
    function GetAge( obj) {
        let identityCard= $(obj).val();
        console.log(identityCard)
        var len = (identityCard + "").length;
        if (len == 0) {
            return 0;
        } else {
            if ((len != 15) && (len != 18))//身份证号码只能为15位或18位其它不合法
            {
                return 0;
            }
        }
        var strBirthday = "";
        if (len == 18)//处理18位的身份证号码从号码中得到生日和性别代码
        {
            strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
        }
        if (len == 15) {
            strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
        }
        //时间字符串里，必须是“/”
        var birthDate = new Date(strBirthday);
        var nowDateTime = new Date();
        var age = nowDateTime.getFullYear() - birthDate.getFullYear();
        //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
        if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
            age--;
        }
        console.log(age)
        $('#age').val(age)

    }
    layui.use(['form', 'layer'],
        function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;


            //监听提交
            form.on('submit(add)',
                function (data) {
                    console.log(data.field);
                    //发异步删除数据
                    $.ajaxSetup(
                    {
                        headers:
                            {
                                'X-CSRF-Token': $('input[name="_token"]').val()
                            }
                    });
                    let url = '/families';
                    $.ajax({
                        url: url,
                        data: data.field,
                        type: 'POST',
                        dataType: "json",
                        success: function () {
                            console.log('success')
                            layer.alert('添加成功',{icon:6,time:5000},function () {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口数据表进行刷新
                                parent.layui.table.reload('test');
                            });
                        },
                        error: function (err) {
                            console.log(err);
                            layer.msg('保存失败', {icon: 2, time: 1000});

                        }
                    });
                    //ajax 异步调用会发生再这个语句之后，返回false可以避免异步返回时页面发生了刷新
                    return false;
                });

            form.verify({
                phonemsg: function(value, item){ //value：表单的值、item：表单的DOM对象
                    if(value!=""){  //值不是空的时候再去走验证
                        if(!/^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/.test(value)){
                            return '手机号格式不正确';
                        }
                    }
                }
            });
        });
</script>
</body>

</html>
